package soar.template.springboot.service.hello.job;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import soar.template.springboot.service.hello.appconfig.AppConfig;

@Component
public class QuartzJob
{
    private static final Logger logger = LoggerFactory
            .getLogger(QuartzJob.class);

    @Resource(name = "mainEntityManagerFactory")
    private EntityManagerFactory mainEntityManagerFactory;

    @Resource(name = "appconfig")
    private AppConfig appconfig;

    protected void execute()
    {
        // logger.info("begin");

        EntityManager main_em = null;

        try
        {
            main_em = mainEntityManagerFactory.createEntityManager();

            main_em.getTransaction().begin();

            main_em.getTransaction().commit();

            // logger.info("alipay.url: {}", appconfig.alipay.url);
        }
        catch (Exception e)
        {
            if (e.getMessage() != null)
                logger.error("fail: {}", e);
            try
            {
                if (main_em != null && main_em.getTransaction().isActive())
                    main_em.getTransaction().rollback();
            }
            catch (Exception er)
            {
                logger.error("rollback fail: {}", er);
            }
        }
        finally
        {
            if (main_em != null)
                main_em.close();
        }

        // logger.info("end");
    }
}
